import java.util.Arrays;

public class Demo3_5 {
    //二分查找
    public static int binarySearch(int[] array, int n) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (array[mid] < n) {
                left = mid + 1;
            } else if (array[mid] > n) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 5, 6, 4, 10, 11};
        Arrays.sort(arr);//对数组进行排序
        int index = binarySearch(arr, 6);
        System.out.println(index);
    }
}
